﻿using DataLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Common;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;
using System.Data;

namespace TicketSystem
{
    public partial class Form_Checkout : Form
    {
        private TicketSystemEntities tse = new TicketSystemEntities();
        public Form_Checkout()
        {
            InitializeComponent();
        }

        private void btn_checkout_Click(object sender, EventArgs e)
        {
            if (DateTime.Compare(Convert.ToDateTime(dTP_Checkout.Text), DateTime.Now) > 0)
            {
                MessageBox.Show("不能对未来日期的账单进行结账");
            }
            else
            {
                bool flag = true;
                bool flag2 = true;
                Checkouts _checkout = new Checkouts();                
                DateTime start = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00");
                DateTime end = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 23:59:59");
                if (tse.Checkouts.Where(u => u.Checkout_at > start && u.Checkout_at < end).Count() > 0)
                {                    
                    _checkout = tse.Checkouts.Single(u => u.Checkout_at >= start && u.Checkout_at <= end);
                    if (DateTime.Compare(_checkout.Checkout_at, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00")) > 0)
                    {
                        MessageBox.Show("今天已经结过账，结账时间为：" + _checkout.Checkout_at);
                        flag = false;
                    }
                }
                else if (tse.Sales.Where(u => u.Created_at > start).Count() == 0)
                {
                    DialogResult dr = MessageBox.Show("今天还没有任何订单，确定要结账吗?", "结账?", MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
                    if (dr == DialogResult.Yes)
                    {
                        flag = true;
                        flag2 = false;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                if (flag)
                {                    
                    try
                    {
                        _checkout.Checkout_at = DateTime.Parse(dTP_Checkout.Text);
                        //_checkout.Checkout_at = DateTime.Now;
                        //_checkout.OperatorID = 1;
                        _checkout.OperatorID = GetOperator.OperatorID;
                        tse.Checkouts.Add(_checkout);
                        if (flag2)//当天是否有订单，没有就不执行下面
                        {                            
                            var todaytotal = tse.Sales.Where(u => u.Checkout_at >= start && u.Checkout_at <= end).ToList();//取出当天所有账单                            
                            foreach (var today in todaytotal)
                            {
                                today.Checkout_at = _checkout.Checkout_at;
                                //foreach (var back in today.Backs)
                                //{
                                //    back.Updated_at = _checkout.Checkout_at;
                                //}
                            }
                            //把今天的退单日期更新成当前结账日期
                            var todayback = tse.Backs.Where(u => u.Created_at >= start && u.Created_at <= end).ToList();
                            foreach(var back in todayback)
                            {
                                back.Updated_at = _checkout.Checkout_at;
                            }
                        }
                        tse.SaveChanges();
                        DialogResult dresult = MessageBox.Show("结账完成,结账时间：" + _checkout.Checkout_at + ",是否打印当天汇总表", "打印汇总表", MessageBoxButtons.OKCancel);
                        if (dresult == DialogResult.OK)
                        {
                            if (Common.PrintDataGridView.DefaultPrinter().Contains("EPSON"))
                            {
                                DialogResult printResult = MessageBox.Show("检测到当前的默认打印机设备为针式打印机,是否需要切换", "选择打印机", MessageBoxButtons.YesNo);
                                if (printResult == DialogResult.Yes)
                                {
                                    Form_SetDefaultPrinter form_setdefaultprint = new Form_SetDefaultPrinter();
                                    form_setdefaultprint.Show();
                                }
                                else
                                {
                                    Print_Doc(_checkout.Checkout_at);
                                }
                            }
                            else
                                Print_Doc(_checkout.Checkout_at);
                            //Print_Doc(_checkout.Checkout_at);
                        }
                        MessageBox.Show("结账完成,结账时间:"+ _checkout.Checkout_at);
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show("结账失败:" + ex. Message);
                    }
                }
            }
        }
        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Print_Doc(DateTime checkoutTime)
        {
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("1", "结账时间");
            dgv.Columns.Add("2", "首道船签单");
            dgv.Columns.Add("3", "首道船非签单");
            dgv.Columns.Add("4", "进园成人票");
            dgv.Columns.Add("5", "进园学生票");
            dgv.Columns.Add("6", "二道船人数");
            dgv.Columns.Add("7", "三道船人数");
            dgv.Columns.Add("8", "首道船退票");
            dgv.Columns.Add("9", "进园成人票退票");
            dgv.Columns.Add("10", "进园学生票退票");
            dgv.Columns.Add("11", "二道船退票");
            dgv.Columns.Add("12", "三道船退票");
            dgv.Columns.Add("13", "现付总金额");
            dgv.Columns.Add("14", "退单总金额");
            dgv.Columns.Add("15", "实收总金额");//现付金额（不含签单金额）-退单金额 

            var SalesDoc = tse.Sales.Where(u => u.Checkout_at == checkoutTime);
            int firstID = 0;
            int endID = 0;
            try
            {
                firstID = SalesDoc.OrderBy(u => u.SaleID).First().SaleID;
                endID = firstID + SalesDoc.Count() - 1;
                //endID = SalesDoc.OrderByDescending(u => u.SaleID).First().SaleID;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                firstID = 0;
                endID = 0;
            }
            int OneTotal = 0;
            int OneSignedTotal = 0;
            int OneUnSignedTotal = 0;
            int TwoTotal = 0;
            int ThreeTotal = 0;
            int ParkTotal = 0;
            int ParkSpecialTotal = 0;
            int BackOneTotal = 0;
            int BackTwoTotal = 0;
            int BackThreeTotal = 0;
            int BackParkTotal = 0;
            int BackParkSpecialTotal = 0;
            decimal PurchaseMoneyTotal = 0;
            var _type = tse.TicketTypes;
            decimal onePrice = _type.Find(1).Price;
            decimal twoPrice = _type.Find(2).Price;
            decimal threePrice = _type.Find(3).Price;
            decimal parkPrice = _type.Find(4).Price;
            decimal parkSpecialPrice = _type.Find(4).SpecialPrice;
            foreach (var sale in SalesDoc)
            {
                OneTotal += sale.TicketOne;
                OneSignedTotal += sale.SignedPeople;
                TwoTotal += sale.TicketTwo;
                ThreeTotal += sale.TicketThree;
                ParkTotal += sale.TicketPark;
                ParkSpecialTotal += sale.TicketParkSpecial;
                PurchaseMoneyTotal += sale.PurchaseMoney;
            }
            OneUnSignedTotal = OneTotal - OneSignedTotal;//非签单人数
            var BackDoc = tse.Backs.Where(u => u.Updated_at == checkoutTime);
            decimal BackMoneyTotal = 0;
            foreach (var back in BackDoc)
            {
                BackOneTotal += back.BackOne;
                BackTwoTotal += back.BackTwo;
                BackThreeTotal += back.BackThree;
                BackParkTotal += back.BackPark;
                BackParkSpecialTotal += back.BackParkSpecial;
                BackMoneyTotal += back.BackMoney;
            }
            decimal TotalMoney = PurchaseMoneyTotal - BackMoneyTotal;
            dgv.Rows[0].Cells[0].Value = checkoutTime;
            dgv.Rows[0].Cells[1].Value = OneSignedTotal;
            dgv.Rows[0].Cells[2].Value = OneUnSignedTotal;
            dgv.Rows[0].Cells[3].Value = ParkTotal;
            dgv.Rows[0].Cells[4].Value = ParkSpecialTotal;
            dgv.Rows[0].Cells[5].Value = TwoTotal;
            dgv.Rows[0].Cells[6].Value = ThreeTotal;
            dgv.Rows[0].Cells[7].Value = BackOneTotal;
            dgv.Rows[0].Cells[8].Value = BackParkTotal;
            dgv.Rows[0].Cells[9].Value = BackParkSpecialTotal;
            dgv.Rows[0].Cells[10].Value = BackTwoTotal;
            dgv.Rows[0].Cells[11].Value = BackThreeTotal;
            dgv.Rows[0].Cells[12].Value = PurchaseMoneyTotal;
            dgv.Rows[0].Cells[13].Value = BackMoneyTotal;
            dgv.Rows[0].Cells[14].Value = TotalMoney;
            Common.PrintDataGridView.Print(dgv, "结账汇总表", "结账：" + checkoutTime + ",开票流水号区间：" + firstID + "--" + endID, 0, 0);

            //DataGridView dgv2 = new DataGridView();
            //dgv2.Columns.Add("1", "首道船现付");
            //dgv2.Columns.Add("2", "签单");
            //dgv2.Columns.Add("3", "金额");
            //dgv2.Columns.Add("4", "进园成人");
            //dgv2.Columns.Add("5", "进园学生");
            //dgv2.Columns.Add("6", "金额");
            //dgv2.Columns.Add("7", "二道船人数");
            //dgv2.Columns.Add("8", "二金额");
            //dgv2.Columns.Add("9", "三道船人数");
            //dgv2.Columns.Add("10", "金额");
            //dgv2.Columns.Add("11", "合计金额");
            //string oneMoney = (OneUnSignedTotal * onePrice).ToString();
            //string twoMoney = (TwoTotal * twoPrice).ToString();
            //string threeMoney = (ThreeTotal * threePrice).ToString();
            //string parkMoney = (ParkTotal * parkPrice).ToString();
            //string parkSpecialMoney = (ParkSpecialTotal * parkSpecialPrice).ToString();
            //dgv2.Rows[0].Cells[0].Value = OneUnSignedTotal;
            //dgv2.Rows[0].Cells[1].Value = OneSignedTotal;
            //dgv2.Rows[0].Cells[2].Value = oneMoney;
            //dgv2.Rows[0].Cells[3].Value = ParkTotal;
            //dgv2.Rows[0].Cells[4].Value = ParkSpecialTotal;
            //dgv2.Rows[0].Cells[5].Value = parkMoney;
            //dgv2.Rows[0].Cells[6].Value = TwoTotal;
            //dgv2.Rows[0].Cells[7].Value = twoMoney;
            //dgv2.Rows[0].Cells[8].Value = ThreeTotal;
            //dgv2.Rows[0].Cells[9].Value = threeMoney;
            //dgv2.Rows[0].Cells[10].Value = PurchaseMoneyTotal;
            //Common.PrintDataGridView.Print(dgv2, "收入汇总表", "时间：" + checkoutTime, 0, 0);

            //DataGridView dgv3 = new DataGridView();
            //dgv3.Columns.Add("1", "首道船现付");
            //dgv3.Columns.Add("2", "签单");
            //dgv3.Columns.Add("3", "金额");
            //dgv3.Columns.Add("4", "进园成人");
            //dgv3.Columns.Add("5", "进园学生");
            //dgv3.Columns.Add("6", "金额");
            //dgv3.Columns.Add("7", "二道船人数");
            //dgv3.Columns.Add("8", "金额");
            //dgv3.Columns.Add("9", "三道船人数");
            //dgv3.Columns.Add("10", "金额");
            //dgv3.Columns.Add("11", "合计金额");
            //string oneBackMoney = (BackOneTotal * onePrice).ToString();
            //string twoBackMoney = (BackTwoTotal * twoPrice).ToString();
            //string threeBackMoney = (BackThreeTotal * threePrice).ToString();
            //string parkBackMoney = (BackParkTotal * parkPrice).ToString();
            //string parkBackSpecialMoney = (BackParkSpecialTotal * parkSpecialPrice).ToString();
            //string parkTotalBackMoney = ((BackParkTotal * parkPrice) + (BackParkSpecialTotal * parkSpecialPrice)).ToString();
            //dgv3.Rows[0].Cells[0].Value = BackOneTotal;
            //dgv3.Rows[0].Cells[1].Value = OneSignedTotal;
            //dgv3.Rows[0].Cells[2].Value = oneBackMoney;
            //dgv3.Rows[0].Cells[3].Value = BackParkTotal;
            //dgv3.Rows[0].Cells[4].Value = BackParkSpecialTotal;
            //dgv3.Rows[0].Cells[5].Value = parkTotalBackMoney;
            //dgv3.Rows[0].Cells[6].Value = BackTwoTotal;
            //dgv3.Rows[0].Cells[7].Value = twoBackMoney;
            //dgv3.Rows[0].Cells[8].Value = BackThreeTotal;
            //dgv3.Rows[0].Cells[9].Value = threeBackMoney;
            //dgv3.Rows[0].Cells[10].Value = BackMoneyTotal;
            //Common.PrintDataGridView.Print(dgv3, "退票汇总表", "时间：" + checkoutTime, 0, 0);

            //DataGridView dgv4 = new DataGridView();
            //dgv4.Columns.Add("1", "入园成人");
            //dgv4.Columns.Add("2", "入园优惠");
            //dgv4.Columns.Add("3", "金额");
            //dgv4.Columns.Add("4", "二道船人数");
            //dgv4.Columns.Add("5", "金额");
            //dgv4.Columns.Add("6", "三道船人数");
            //dgv4.Columns.Add("7", "金额");
            //dgv4.Columns.Add("8", "合计金额");
            //dgv4.Rows[0].Cells[0].Value = ParkTotal;
            //dgv4.Rows[0].Cells[1].Value = ParkSpecialTotal;
            //dgv4.Rows[0].Cells[2].Value = (decimal.Parse(parkMoney) + decimal.Parse(parkSpecialMoney)).ToString();
            //dgv4.Rows[0].Cells[3].Value = TwoTotal;
            //dgv4.Rows[0].Cells[4].Value = twoMoney;
            //dgv4.Rows[0].Cells[5].Value = ThreeTotal;
            //dgv4.Rows[0].Cells[6].Value = threeMoney;
            //dgv4.Rows[0].Cells[7].Value = (decimal.Parse(parkSpecialMoney) + decimal.Parse(parkMoney) + decimal.Parse(twoMoney) + decimal.Parse(threeMoney)).ToString();
            //Common.PrintDataGridView.Print(dgv4, "颐和园入园对账汇总表", "时间" + checkoutTime, 0, 0);


            //DataGridView dgv5 = new DataGridView();
            //dgv5.Columns.Add("1", "收款金额");
            //dgv5.Columns.Add("2", "退票金额");
            //dgv5.Columns.Add("3", "实际交款金额");
            //dgv5.Rows[0].Cells[0].Value = PurchaseMoneyTotal;
            //dgv5.Rows[0].Cells[1].Value = BackMoneyTotal;
            //dgv5.Rows[0].Cells[2].Value = TotalMoney;
            //Common.PrintDataGridView.Print(dgv5, "当日缴款金额汇总表", "时间" + checkoutTime, 0, 0);
        }
    }
}
